#include<bits/stdc++.h>
using namespace std;
const int M =2e5+10,N=1e5+10;;
struct edge
{
    int a,b,w;
    bool operator<(const edge&s)
    {
        return w<s.w;
    }
}edges[M];
int p[N];
int find(int x)
{
    if(x!=p[x])p[x]=find(p[x]);
    return p[x];
}
int n,m,res,cnt;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>edges[i].a>>edges[i].b>>edges[i].w;
    }
    sort(edges+1,edges+m+1);
    for(int i=1;i<=n;i++)p[i]=i;
    for(int i=1;i<=m;i++)
    {
        int a=edges[i].a,b=edges[i].b,w=edges[i].w;
        a=find(a),b=find(b);
        if(a!=b)
        {
            p[a]=b;
            res+=w;
            cnt++;
        }
    }
    if(cnt<n-1)cout<<"impossible";
    else cout<<res;
    return 0;
}
